﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace CommonLib
{
    public class FormStyle
    {
        /// <summary>
        /// 打开窗体(可防止重复打开)
        /// </summary>
        /// <param name="frm">打开的winForm窗体实例</param>
        /// <param name="IFDialog">是否是Dialog</param>
        public static void OpenForm(Form owner, System.Windows.Forms.Form frm, bool IFDialog)
        {
            if (frm == null) return;
            foreach (System.Windows.Forms.Form f in Application.OpenForms)
            {
                if (f.Name == frm.Name)
                {
                    if (f.WindowState == FormWindowState.Minimized)
                    {
                        f.WindowState = FormWindowState.Normal;
                    }
                    f.Activate();
                    //if (IFDialog)
                    //{
                    //    f.ShowDialog(owner);
                    //}
                    //else
                    //{
                    //    f.Show(owner);
                    //}
                    frm.Dispose();
                    System.GC.Collect();
                    System.GC.WaitForPendingFinalizers();
                    return;
                }
            }
            frm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            if (IFDialog)
            {
                frm.ShowDialog(owner);
                frm.Icon = owner.Icon;
            }
            else
            {
                frm.Show(owner);
                frm.Icon = owner.Icon;
            }
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
        }
        public static void OpenForm(System.Windows.Forms.Form frm, bool IFDialog)
        {
            if (frm == null) return;
            foreach (System.Windows.Forms.Form f in Application.OpenForms)
            {
                if (f.Name == frm.Name)
                {
                    if (f.WindowState == FormWindowState.Minimized)
                    {
                        f.WindowState = FormWindowState.Normal;
                    }
                    f.Activate();
                    //if (IFDialog)
                    //{
                    //    f.ShowDialog(owner);
                    //}
                    //else
                    //{
                    //    f.Show(owner);
                    //}
                    frm.Dispose();
                    System.GC.Collect();
                    System.GC.WaitForPendingFinalizers();
                    return;
                }
            }
            frm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            if (IFDialog)
            {
                frm.ShowDialog();
            }
            else
            {
                frm.Show();
            }
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
        }
        public static void OpenForm(System.Windows.Forms.Form frm,object o, bool IFDialog)
        {
            if (frm == null) return;
            foreach (System.Windows.Forms.Form f in Application.OpenForms)
            {
                if (f.Name == frm.Name)
                {
                    if (f.WindowState == FormWindowState.Minimized)
                    {
                        f.WindowState = FormWindowState.Normal;
                    }
                    f.Activate();
                    frm.Dispose();
                    System.GC.Collect();
                    System.GC.WaitForPendingFinalizers();
                    return;
                }
            }
            frm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            if (IFDialog)
            {
                frm.ShowDialog();
            }
            else
            {
                frm.Show();
            }
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
        }
        public static void CloseForm(System.Windows.Forms.Form frm, bool IsDispose)
        {
            if (frm == null) return;
            foreach (System.Windows.Forms.Form f in Application.OpenForms)
            {
                if (f.Name == frm.Name)
                {
                    if (IsDispose)
                    {
                        frm.Dispose();
                    }
                    else
                    {
                        frm.Close();
                    }
                    System.GC.Collect();
                    System.GC.WaitForPendingFinalizers();
                    return;
                }
            }
        }
        #region 拖动
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        public static void FormPan(IntPtr hwnd)
        {
            ReleaseCapture();
            SendMessage(hwnd, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
        #endregion
    }
    /// <summary>
    /// 登陆窗体动画效果
    /// </summary>
    public class AnimateWindowFrom
    {
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
        const int AW_HOR_POSITIVE = 0x0001;
        const int AW_HOR_NEGATIVE = 0x0002;
        const int AW_VER_POSITIVE = 0x0004;
        const int AW_VER_NEGATIVE = 0x0008;
        const int AW_CENTER = 0x0010;
        const int AW_HIDE = 0x10000;
        const int AW_ACTIVATE = 0x20000;
        const int AW_SLIDE = 0x40000;
        const int AW_BLEND = 0x80000;


        /// <summary>
        /// 窗体由中心点放大
        /// </summary>
        /// <param name="hwnd"></param>
        public static void WindowZoomIn(IntPtr hwnd)
        {
            AnimateWindow(hwnd, 1000, AW_CENTER | AW_ACTIVATE);
        }
        /// <summary>
        /// 窗体由中心向四周缩小
        /// </summary>
        /// <param name="hwnd"></param>
        public static void WindowZoomOut(IntPtr hwnd)
        {
            AnimateWindow(hwnd, 1000, AW_ACTIVATE | AW_HIDE | AW_CENTER);
        }
    }
    /// <summary>
    /// 窗体阴影效果
    /// </summary>
    public class FormShadow
    {
        public static void ShadowStyle(IntPtr hwnd)
        {
            SetClassLong(hwnd, GCL_STYLE, GetClassLong(hwnd, GCL_STYLE) | CS_DropSHADOW); //API函数加载，实现窗体边框阴影效果
        }
        #region 窗体边框阴影效果变量
        const int CS_DropSHADOW = 0x20000;
        const int GCL_STYLE = (-26);
        //声明Win32 API
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassLong(IntPtr hwnd, int nIndex);
        #endregion
    }
}
